package com.miplus.ccrm.workflow.controller;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.PageData;
import com.miplus.ccrm.common.R;
import com.miplus.ccrm.workflow.model.dto.workflow.*;
import com.miplus.ccrm.workflow.model.vo.WorkflowDetailsVO;
import com.miplus.ccrm.workflow.model.vo.WorkflowVO;
import com.miplus.ccrm.workflow.model.vo.WorkflowVersionVO;
import com.miplus.ccrm.workflow.service.WorkflowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 话术管理Controller
 */
@RestController
@RequestMapping("/workflow")
@Api(tags = "话术管理")
public class WorkflowController {

    @Resource
    private WorkflowService workflowService;

    /**
     * 创建话术
     */
    @PostMapping("/create")
    @ApiOperation("创建话术")
    public R<CmdResVo> createWorkflow(@RequestBody WorkflowCreateDTO workflowCreateDTO) {
        String userId = getUserId();
        return R.Success(workflowService.createWorkflow(workflowCreateDTO, userId));
    }

    /**
     * 更新话术(相当于保存)
     * 每次保存，都会生成一个新版本，版本号会递增
     */
    @PostMapping("/update")
    @ApiOperation("更新话术")
    public R<CmdResVo> updateWorkflow(@RequestBody WorkflowUpdateDTO workflowUpdateDTO) {
        String userId = getUserId();
        return R.Success(workflowService.updateWorkflow(workflowUpdateDTO, userId));
    }

    /**
     * 删除话术(话术id和版本号对应)
     */
    @PostMapping("/delete")
    @ApiOperation("删除话术")
    public R<CmdResVo> deleteWorkflow(@RequestParam String id, @RequestParam Integer version) {
        String userId = getUserId();
        return R.Success(workflowService.deleteWorkflow(id, version, userId));
    }

    /**
     * 分页查询话术设计列表，不针对版本表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询话术列表(展示当前版本的状态包含上架，发布,申请)")
    public R<PageData<WorkflowVO>> getWorkflowList(@RequestBody WorkflowQueryDTO query) {
       return R.SuccessPage(workflowService.getWorkflowList(query));
    }


    /**
     * 分页查询话术审核和已发布列表，针对版本表来展示
     */
    @PostMapping("/detail/list")
    @ApiOperation("分页查询版本列表展示所有")
    public R<PageData<WorkflowDetailsVO>> getWorkFlowDeatislList(@RequestBody WorkflowQueryDTO query) {
        return R.SuccessPage(workflowService.getWorkflowDetailsList(query));
    }

    /**
     * 查询话术详情
     */
    @PostMapping("/detail}")
    @ApiOperation("查询话术详情")
    public R<WorkflowVO> getWorkflowDetail(@RequestBody WorkflowDetailDTO workflowDetailDTO) {
        WorkflowVO workflow = workflowService.getWorkflowDetail(workflowDetailDTO);
        return R.Success(workflow);
    }

    /**
     * 复制话术
     */
    @PostMapping("/copy")
    @ApiOperation("复制话术")
    public R<CmdResVo> copyWorkflow(@RequestBody  WorkflowCopyDTO workflowCopyDTO) {
        String userId = getUserId();
        return R.Success(workflowService.copyWorkflow(workflowCopyDTO, userId));
    }

    /**
     * 统一话术状态管理接口
     */
    @PostMapping("/status/change")
    @ApiOperation("统一话术状态变更接口")
    public R<CmdResVo> changeWorkflowStatus(@RequestBody WorkflowStatusChangeDTO statusChangeDTO) {
        String userId = getUserId();
        return R.Success(workflowService.changeWorkflowStatus(statusChangeDTO, userId));
    }


    /**
     * 撤回审核
     */
    @PostMapping("/withdraw")
    @ApiOperation("撤回审核")
    public R<CmdResVo> withdrawWorkflow(@RequestBody WorkflowWithdrawDTO workflowWithdrawDTO) {
        String userId = getUserId();
        return R.Success(workflowService.withdrawWorkflow(workflowWithdrawDTO, userId));
    }

    /**
     * 导出话术 todo
     */
    @PostMapping("/export")
    @ApiOperation("导出话术")
    public CmdResVo exportWorkflow(@PathVariable String id, HttpServletRequest request) {
        return null;

    }

    /**
     * 导入话术 todo
     */
    @PostMapping("/import")
    @ApiOperation("导入话术")
    public CmdResVo importWorkflow(@RequestBody WorkflowImportDTO workflowImportDTO, HttpServletRequest request) {
        return null;
    }

    /**
     * 获取用户话术列表
     */
    @PostMapping("/user-workflows")
    @ApiOperation("获取用户话术列表")
    public R<PageData<WorkflowVO>> getUserWorkflows(@RequestBody WorkflowQueryDTO query) {
        String userId = getUserId();
        return R.SuccessPage(workflowService.getUserWorkflows(userId));
    }

    /**
     * 切换话术历史版本
     */
    @PostMapping("/switch-version")
    @ApiOperation("切换话术历史版本")
    public R<CmdResVo> switchVersion(@RequestBody WorkflowSwitchVersionDTO workflowSwitchVersionDTO, HttpServletRequest request) {
        String userId = getUserId();
        return R.Success(workflowService.switchVersion(workflowSwitchVersionDTO, userId));
    }

    /**
     * 获取用户话术历史版本
     */
    @GetMapping("/user-workflow-versions")
    @ApiOperation("获取用户话术历史版本")
    public R<List<WorkflowVersionVO>> getUserWorkflowVersions(@RequestParam String workflowId) {
        String userId = getUserId();
        return R.Success(workflowService.getUserWorkflowVersions(workflowId, userId));
    }

    /**
     * 编辑话术历史版本
     */
    @PostMapping("/edit-version")
    @ApiOperation("编辑话术历史版本")
    public R<CmdResVo> editWorkflowVersion(@RequestBody WorkflowVersionUpdateDTO workflowVersionUpdateDTO) {
        String userId = getUserId();
        return R.Success(workflowService.editWorkflowVersion(workflowVersionUpdateDTO, userId));
    }



    /**
     * 从请求中获取用户ID
     */
    private String getUserId() {
        // 这里应该从token或session中获取用户ID
        // 暂时返回一个默认值
        return "default_user";
    }
}